#include "cmenubutton.h"
#include "ui_cmenubutton.h"

#include <QBitmap>

Cmenubutton::Cmenubutton(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Cmenubutton)
{
    ui->setupUi(this);

    ui->cmenuBttonImg->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
    QFont f;
    f.setPointSize(6);
    ui->cmenuButtonLb->setFont(f);

    QBitmap bmp(this->size());
    bmp.fill();
    QPainter p(&bmp);
    p.setPen(Qt::NoPen);
    p.setBrush(Qt::black);
    p.drawRoundedRect(bmp.rect(),2,2);
    setMask(bmp);
}

void Cmenubutton::setNormalImg(QString s) {
    normalImg = s;
    ui->cmenuBttonImg->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
}

void Cmenubutton::sethoverImg(QString s) {
    hoverImg = s;
}

void Cmenubutton::setPressImg(QString s) {
    pressImg = s;
}

void Cmenubutton::setButtonTxt(QString s) {
    ui->cmenuButtonLb->setText(s);
}

void Cmenubutton::setButtonNolmalStatus()
{
    status = NORMAL;
    ui->cmenuBttonImg->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
    ui->cmenuButtonLb->setStyleSheet("color:white");
}

void Cmenubutton::setButtonSelectStatus() {
    status = PRESS;
    ui->cmenuBttonImg->setStyleSheet("QLabel{border-image:url(:" + pressImg + ")}");
    ui->cmenuButtonLb->setStyleSheet("color:#4381ff");
}

void Cmenubutton::enterEvent(QEvent *)
{
    setStyleSheet("background-color:#474747;");
}

void Cmenubutton::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        mouse_press = true;
        if (needSelect) {
            if (status == PRESS) {
                ui->cmenuBttonImg->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
                ui->cmenuButtonLb->setStyleSheet("color:white");
                status = NORMAL;
            }else {
                ui->cmenuBttonImg->setStyleSheet("QLabel{border-image:url(:" + pressImg + ")}");
                ui->cmenuButtonLb->setStyleSheet("color:#4381ff");
                status = PRESS;
            }
        }else {
            ui->cmenuBttonImg->setStyleSheet("QLabel{border-image:url(:" + pressImg + ")}");
            ui->cmenuButtonLb->setStyleSheet("color:#4381ff");
            status = PRESS;
        }

        emit btnClicked();
    }
}

void Cmenubutton::mouseReleaseEvent(QMouseEvent *event)
{
    if(mouse_press&&this->rect().contains(event->pos()))
    {
        mouse_press = false;
        if (!needSelect) {
            status = HOVER;
            ui->cmenuBttonImg->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
            ui->cmenuButtonLb->setStyleSheet("color:white");
        }
    }
}

void Cmenubutton::leaveEvent(QEvent *)
{
    if (!needSelect) {
        status = NORMAL;
        ui->cmenuBttonImg->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
        ui->cmenuButtonLb->setStyleSheet("color:white");
    }else {
        if (status != PRESS) {
            ui->cmenuBttonImg->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
            ui->cmenuButtonLb->setStyleSheet("color:white");
        }
    }

    setStyleSheet("background-color: rgb(28, 30, 32);");
}


Cmenubutton::~Cmenubutton()
{
    delete ui;
}
